package com.jdz.gxq.info.admin.role.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.role.bo.User;
import com.jdz.gxq.info.admin.role.form.UserForm;
import com.jdz.gxq.info.admin.role.service.UserService;
import com.jdz.gxq.info.common.action.CommonDispatchAction;
import com.jdz.gxq.info.common.bo.PageBean;

public class UserAction extends CommonDispatchAction {

	private UserService userService = null;
	 
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		UserForm userForm = (UserForm) form;

		String name = userForm.getName();

		int totalCount = userService.getUsersCount(name);

		int currentPage = userForm.getPageIndex();

		PageBean pageBean = new PageBean(totalCount, currentPage);

		userForm.setPageIndex(pageBean.getCurrentPage());

		Map<String, Object> params = new HashMap<String, Object>();

		// 分页用
		params.put("startRow", pageBean.getPageStartRow() - 1);
		params.put("rowSize", pageBean.getPageSize());
		params.put("name", name);

		List<User> userList = userService.getUsers(params);

		request.setAttribute("userList", userList);
		request.setAttribute("pageBean", pageBean);

		return mapping.findForward("success");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		UserForm userForm = (UserForm) form;

		User user = userForm.getUser();

		// user名重复验证(其他后台逻辑验证也类似处理)
		if (!userService.checkUserName(user, 1)) {
			ActionMessages messages = new ActionMessages();
			messages.add("user.name",
					new ActionMessage("error.userNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		user.setModifiedBy(getUserId(request));

		userService.insertUser(user);

		return mapping.findForward("success");
	}
	
	public ActionForward gotoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		UserForm userForm = (UserForm) form;

		User user = userService.getUserById(userForm.getId());

		userForm.setUser(user);

		return mapping.findForward("success");
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		UserForm userForm = (UserForm) form;

		User user = userForm.getUser();

		// user名重复验证(其他后台逻辑验证也类似处理)
		if (!userService.checkUserName(user, 2)) {
			ActionMessages messages = new ActionMessages();
			messages.add("user.name",
					new ActionMessage("error.userNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		user.setModifiedBy(getUserId(request));

		userService.updateUser(user);

		return mapping.findForward("success");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		String useridsStr = request.getParameter("id");
		
		String[] userids = useridsStr.split(",");
		
		for (String userId : userids) {
			userService.deleteUser(Integer.parseInt(userId));
		}

		return mapping.findForward("success");
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}

